//
//  HLFriendsCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLFriendsCell.h"
#import "HLFriendListViewController.h"
#import "HLUserOptionRequest.h"

@interface HLFriendsCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIImageView *countryView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) QMUIFloatLayoutView *labelsView;
@property (nonatomic, strong) QMUIButton *realView;
@property (nonatomic, strong) QMUIButton *vipView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) QMUIButton *attentionButton;
@property (nonatomic, strong) QMUIButton *cancelAttentionButton;
@property (nonatomic, strong) QMUIButton *cancelBlackButton;
@property (nonatomic, strong) QMUIButton *chatButton;

@end

@implementation HLFriendsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 24;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.image = [HLUIHelper avatarPlaceholder];
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(48);
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(12);
        make.bottom.mas_equalTo(-12);
    }];
    self.countryView = [[UIImageView alloc] init];
    _countryView.image = UIImageMake(@"country_cn_icon");
    _countryView.layer.masksToBounds = YES;
    _countryView.layer.cornerRadius = 7;
    [self.contentView addSubview:_countryView];
    [_countryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(_avatarView);
        make.size.mas_equalTo(14);
    }];
    
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"聊人生阿姨有没有啊";
    _nicknameLabel.font = UIFontMediumMake(14);
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView.mas_right).offset(12);
        make.top.equalTo(_avatarView).offset(4);
        make.right.mas_equalTo(-104);
    }];
    
    self.realView = [HLUIHelper realIconView];
    self.vipView = [HLUIHelper vipIconView];
    self.labelsView = [[QMUIFloatLayoutView alloc] init];
    //_labelsView.padding = UIEdgeInsetsMake(0, 4, 0, 4);
    _labelsView.itemMargins = UIEdgeInsetsMake(0, 1, 0, 1);
    _labelsView.minimumItemSize = CGSizeMake(18, 18);// 以2个字的按钮作为最小宽度
    [self.contentView addSubview:_labelsView];
    [_labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel.mas_right).offset(4);
        make.right.mas_equalTo(-6);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(_nicknameLabel);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.font = UIFontMake(12);
    _contentLabel.textColor = HLTextColor2;
    _contentLabel.numberOfLines = 1;
    _contentLabel.text = @"错误的决定比没有决定要好";
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.bottom.equalTo(_avatarView).offset(-4);
        make.right.mas_equalTo(-104);
    }];
    
    
    self.attentionButton = [[QMUIButton alloc] init];
    [_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    [_attentionButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_attentionButton setCornerRadius:14];
    [_attentionButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_attentionButton setBackgroundColor:HLMainColor];
    [_attentionButton.titleLabel setFont:UIFontMediumMake(12)];
    [_attentionButton addTarget:self action:@selector(attentionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_attentionButton];
    [_attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
    
    self.cancelAttentionButton = [[QMUIButton alloc] init];
    [_cancelAttentionButton setTitle:@"取消关注" forState:UIControlStateNormal];
    [_cancelAttentionButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    [_cancelAttentionButton setCornerRadius:14];
    [_cancelAttentionButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_cancelAttentionButton setBackgroundColor:HLBackgroundColor];
    [_cancelAttentionButton.titleLabel setFont:UIFontMediumMake(12)];
    [_cancelAttentionButton addTarget:self action:@selector(cancelAttentionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_cancelAttentionButton];
    [_cancelAttentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];

    
    self.cancelBlackButton = [[QMUIButton alloc] init];
    [_cancelBlackButton setTitle:@"取消拉黑" forState:UIControlStateNormal];
    [_cancelBlackButton setTitleColor:HLTextColor2 forState:UIControlStateNormal];
    [_cancelBlackButton setCornerRadius:14];
    [_cancelBlackButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_cancelBlackButton setBackgroundColor:HLBackgroundColor];
    [_cancelBlackButton.titleLabel setFont:UIFontMediumMake(12)];
    [_cancelBlackButton addTarget:self action:@selector(cancelBlackAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_cancelBlackButton];
    [_cancelBlackButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
    
    self.chatButton = [[QMUIButton alloc] init];
    [_chatButton setTitle:@"私聊" forState:UIControlStateNormal];
    [_chatButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_chatButton setCornerRadius:14];
    [_chatButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
    [_chatButton setBackgroundColor:HLAideColor1];
    [_chatButton.titleLabel setFont:UIFontMediumMake(12)];
    [_chatButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_chatButton];
    [_chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
}


- (void)setModel:(HLFriendsModel *)model
{
    _model = model;
    
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.fuAvatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _nicknameLabel.text = model.fuNickname;
    _contentLabel.text = model.fuSelfSign;
    
    _attentionButton.hidden = YES;
    _cancelAttentionButton.hidden = YES;
    _chatButton.hidden = YES;
    _cancelBlackButton.hidden = YES;
    
    switch (model.type) {
        case HLFriendTypeMine:
            _chatButton.hidden = NO;
            break;
        case HLFriendTypeFans:
            if ([model.followType isEqualToString:@"1"]){
                _attentionButton.hidden = NO;
            } else {
                _cancelAttentionButton.hidden = NO;
            }
            
            break;
        case HLFriendTypeAttention:
            _cancelAttentionButton.hidden = NO;
            break;
        case HLFriendTypeBlack:
            _cancelBlackButton.hidden = NO;
            break;
    }
    
    [_labelsView qmui_removeAllSubviews];
    [_labelsView addSubview:_realView];
    [_labelsView addSubview:_vipView];
}

- (void)chatAction
{
    [HLUIHelper chatWithUserId:_model.fuId controller:self.qmui_viewController];
}

- (void)cancelAttentionAction
{
    [self attentionRequestWithType:HLUserOptionTypeUnFollow];
}

- (void)attentionAction
{
    [self attentionRequestWithType:HLUserOptionTypeFollow];
}

- (void)cancelBlackAction
{
    MJWeakSelf
    [HLUIHelper blackWithUserId:_model.fuId black:YES callback:^(BOOL isConfirm) {
        if (isConfirm){
            [weakSelf.qmui_tableView reloadData];
        }
    }];
}

- (void)attentionRequestWithType:(HLUserOptionType)type
{
    MJWeakSelf
    
    [HLUIHelper followWithUserId:_model.fuId follow:type == HLUserOptionTypeUnFollow callback:^(BOOL isConfirm) {
        if (isConfirm){
            HLFriendListViewController *vc = (HLFriendListViewController *)[weakSelf qmui_viewController];
            [vc.datasources removeObject:weakSelf.model];
            [weakSelf.qmui_tableView reloadData];
        }
    }];
}


@end
